//
//  NSObject+ZHModelCache.m
//  ToolsEnginer
//
//  Created by 正合适 on 2017/6/1.
//  Copyright © 2017年 正合适. All rights reserved.
//

#import "NSObject+ZHModelCache.h"
#import "ZHCache.h"
#import "MJExtension.h"

// 表名
#define kTableName @"Table_ZHModelCache"

#define kKeyName   NSStringFromClass([self class])

@implementation NSObject (ZHModelCache)
#pragma mark - 缓存到数据库
- (void)toCache {
    [ZHCache cacheWithKey:kKeyName value:self.mj_JSONString table:kTableName];
}

- (void)toCacheWithKey:(NSString *)key {
    [ZHCache cacheWithKey:key value:self.mj_JSONString table:kTableName];
}

#pragma mark - 从缓存获取
+ (id)fromCache {
    NSString *jsonString = [ZHCache fromCacheWithKey:kKeyName table:kTableName];
    return jsonString.length == 0 ? nil : [[self class] mj_objectWithKeyValues:jsonString];
}

+ (id)fromCacheWithKey:(NSString *)key {
    NSString *jsonString = [ZHCache fromCacheWithKey:key table:kTableName];
    return jsonString.length == 0 ? nil : [[self class] mj_objectWithKeyValues:jsonString];
}

#pragma mark - 删除缓存
+ (void)deleteCache {
    [ZHCache deleteCacheWithKey:kKeyName table:kTableName];
}

+ (void)deleteCacheWithKey:(NSString *)key {
    [ZHCache deleteCacheWithKey:key table:kTableName];
}

#pragma mark - 清空所有数据
+ (void)clearCache {
    [ZHCache clearCacheWithTable:kTableName];
}

@end
